package main.katalog.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

public class item {
	private String type;
	private int itemID;
	private String i_name;
	private String y_name;
	private int ownerID;
	private String owner;
	private int isLent;
	private String whoLentIt;
	private String desc;
	private Date date;
	
	public item(String type, String i_name, String y_name, int ownerID, String desc){
		this.type = type;
		this.i_name = i_name;
		this.y_name = y_name;
		this.ownerID = ownerID;
		this.desc = desc;
	}
	
	public item(String type, String i_name, String y_name, int ownerID, int isLent, String whoLentIt, String desc){
		this.type = type;
		this.i_name = i_name;
		this.y_name = y_name;
		this.ownerID = ownerID;
		this.isLent = isLent;
		this.whoLentIt = whoLentIt;
		this.desc = desc;
	}
	
	public item() {
		// TODO Auto-generated constructor stub
	}

	public void setItem(item item){
		this.type = item.type;
		this.itemID = item.itemID;
		this.i_name = item.i_name;
		this.y_name = item.y_name;
		this.ownerID = item.ownerID;
		this.owner = item.owner;
		this.isLent = item.isLent;
		this.whoLentIt = item.whoLentIt;
		this.desc = item.desc;	
		this.date = item.date;
	}
	public void getItem(int itemID){
		connection con;
		String sql = "SELECT * FROM item join users WHERE users.userID = item.ownerID AND item.itemID = '" + itemID + "'";

		try {
			con = new connection();
			con.setStatement(con.getConnect().createStatement());
			con.setResultSet(con.getStatement().executeQuery(sql));
			while(con.getResultSet().next()){
				this.type = con.getResultSet().getString("type");
				this.itemID = con.getResultSet().getInt("itemID");
				this.i_name = con.getResultSet().getString("itemName");
				this.y_name = con.getResultSet().getString("yName");
				this.ownerID = con.getResultSet().getInt("ownerID");
				this.owner = con.getResultSet().getString("name");
				this.isLent = con.getResultSet().getInt("isLented");
				this.whoLentIt = con.getResultSet().getString("whoLentIt");
				this.desc = con.getResultSet().getString("desc");
				this.date = con.getResultSet().getDate("createdOn");  
			}
			con.close();
		} catch (Exception e1){
			e1.printStackTrace();
		}
	}
	public boolean addItem(String i_name, String y_name, int ownerID, String desc, String itemType){
		connection con;
		int itemID = getItemID(i_name, y_name, ownerID, itemType);
		if(itemID != -1)
			return false;
		String sql = "INSERT INTO `item`(`type`, `itemName`, `yName`, `ownerID`,`desc`) VALUES ( '" + itemType + "', '" + i_name + "', '"  + y_name + "', '" + ownerID + "', '" + desc + "') ";
		
		try {
			con = new connection();
			con.setStatement(con.getConnect().createStatement());
			con.getConnect().prepareStatement(sql).execute(sql);
			con.close();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return true;
	}
	public void updateItem(int itemID, int isLented, String whoLent){
		connection con;
		String sql = "UPDATE `item` SET `isLented`='"+isLented+"',`whoLentIt`='"+whoLent+"' WHERE `itemID`='"+itemID+"'";
		
		try {
			con = new connection();
			con.setStatement(con.getConnect().createStatement());
			con.getConnect().prepareStatement(sql).execute(sql);
			con.close();
		} catch(Exception e1) {
			e1.printStackTrace();
		}
	}
	public void updateItemDesc(int itemID, String desc){
		connection con;
		String sql = "UPDATE `item` SET `desc`='"+desc+"' WHERE `itemID`='"+itemID+"'";
		
		try {
			con = new connection();
			con.setStatement(con.getConnect().createStatement());
			con.getConnect().prepareStatement(sql).execute(sql);
			con.close();
		} catch(Exception e1) {
			e1.printStackTrace();
		}
	}
	public void updateItemName(int itemID, String i_name){
		connection con;
		String sql = "UPDATE `item` SET `itemName`='"+i_name+"' WHERE `itemID`='"+itemID+"'";
		
		try {
			con = new connection();
			con.setStatement(con.getConnect().createStatement());
			con.getConnect().prepareStatement(sql).execute(sql);
			con.close();
		} catch(Exception e1) {
			e1.printStackTrace();
		}
	}
	public void updateItemYName(int itemID, String y_name){
		connection con;
		String sql = "UPDATE `item` SET `yName`='"+y_name+"' WHERE `itemID`='"+itemID+"'";
		
		try {
			con = new connection();
			con.setStatement(con.getConnect().createStatement());
			con.getConnect().prepareStatement(sql).execute(sql);
			con.close();
		} catch(Exception e1) {
			e1.printStackTrace();
		}
	}
	public int getItemID(String i_name, String y_name, int ownerID, String type){
		int itemID = -1;
		connection con;
		String sql = "SELECT itemID FROM item WHERE itemName = '" + i_name + "' AND yName = '" + y_name + "' AND type = '" + type + "' AND ownerID = '" + ownerID + "'";
		
		try{
			con = new connection();
			con.setStatement(con.getConnect().createStatement());
			con.setResultSet(con.getStatement().executeQuery(sql));
			  if(con.getResultSet().next()){
				  itemID = con.getResultSet().getInt("itemID");
				  con.close();
			  }
			
		} catch (Exception e1){
			e1.printStackTrace();
		}
		return itemID;
	}
	public void deleteItem(){
		connection con;
		String sql = "DELETE FROM `item` WHERE `itemID`='"+ this.itemID + "'";
		try{
			con = new connection();
			con.setStatement(con.getConnect().createStatement());
			con.getConnect().prepareStatement(sql).execute(sql);
			con.close();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
	}
	public List<item> listItem(int ownerID){
		connection con;
		List<item> itemList = new ArrayList<item>();
		String sql = "SELECT * FROM item join users WHERE users.userID = item.ownerID AND item.ownerID = '" + ownerID + "'";
		try{
			con = new connection();
			con.setStatement(con.getConnect().createStatement());
			con.setResultSet(con.getStatement().executeQuery(sql));
			while(con.getResultSet().next()){
				item it = new item();
				it.type = con.getResultSet().getString("type");
				it.itemID = con.getResultSet().getInt("itemID");
				it.i_name = con.getResultSet().getString("itemName");
				it.y_name = con.getResultSet().getString("yName");
				it.ownerID = con.getResultSet().getInt("ownerID");
				it.owner = con.getResultSet().getString("name");
				it.isLent = con.getResultSet().getInt("isLented");
				it.whoLentIt = con.getResultSet().getString("whoLentIt");
				it.desc = con.getResultSet().getString("desc");
				it.date = con.getResultSet().getDate("createdOn");
				itemList.add(it);	  
			}
			con.close();
			  
		} catch (Exception e1){
			e1.printStackTrace();
		}
		return itemList;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public int getItemID() {
		return itemID;
	}

	public void setItemID(int itemID) {
		this.itemID = itemID;
	}

	public String getI_name() {
		return i_name;
	}

	public void setI_name(String i_name) {
		this.i_name = i_name;
	}

	public String getY_name() {
		return y_name;
	}

	public void setY_name(String y_name) {
		this.y_name = y_name;
	}

	public int getOwnerID() {
		return ownerID;
	}

	public void setOwnerID(int ownerID) {
		this.ownerID = ownerID;
	}

	public int getIsLent() {
		return isLent;
	}

	public void setIsLent(int isLent) {
		this.isLent = isLent;
	}

	public String getWhoLentIt() {
		return whoLentIt;
	}

	public void setWhoLentIt(String whoLentIt) {
		this.whoLentIt = whoLentIt;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getOwner() {
		return owner;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}
}
